package com.cennavi.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.cennavi.beans.DevAndLane;
import com.cennavi.beans.DevAndRtic;

/** * @author  作者 :allen
    * @date 创建时间：2017年9月11日 上午11:04:11 
    * @version 1.0 
    */

public class BroadDataGetUtil {
	
	/**
	 * 获取设备id和车道id、方向的映射
	 * 当前量级：100多个key，每个key对应一个小于10的list
	 * @param filePath
	 * @return
	 */
	public static HashMap<String,List<DevAndLane>> getDev_laneMap(String filePath){
		HashMap<String, List<DevAndLane>> dev_laneMap = new HashMap<String, List<DevAndLane>>();
		// 获取设备id和车道id、方向的映射
		File file = new File(filePath);
	    BufferedReader reader = null;
	    try {
			reader = new BufferedReader(new FileReader(file));
			String line = null;
			int lineNum = 1;
			String[] data_eachline = null;
			// 一次读入一行，直到读入null为文件结束
			while ((line = reader.readLine()) != null) {
				if(lineNum > 1){
					data_eachline = line.split(",");
					/*if(data_eachline[0].equals("3034")){
						System.out.println("");
					}*/
					DevAndLane vo = new DevAndLane();
					vo.setDev_id(data_eachline[0]);
					vo.setDev_type(data_eachline[1]);
					vo.setDir(data_eachline[2]);
					/*if(!data_eachline[3].trim().equals("")){
						vo.setLaneNum1(data_eachline[3]);
					}
					if(!data_eachline[4].trim().equals("")){
						vo.setLaneNum2(data_eachline[4]);
					}
					if(!data_eachline[5].trim().equals("")){
						vo.setLaneNum3(data_eachline[5]);
					}
					if(!data_eachline[6].trim().equals("")){
						vo.setLaneNum4(data_eachline[6]);
					}*/
					vo.setLaneNum1(data_eachline[3]);
					if(data_eachline.length == 5){
						vo.setLaneNum2(data_eachline[4]);
					}else if(data_eachline.length == 6){
						vo.setLaneNum2(data_eachline[4]);
						vo.setLaneNum3(data_eachline[5]);
					}else if(data_eachline.length == 7){
						vo.setLaneNum2(data_eachline[4]);
						vo.setLaneNum3(data_eachline[5]);
						vo.setLaneNum4(data_eachline[6]);
					}else if(data_eachline.length == 8){
						vo.setLaneNum2(data_eachline[4]);
						vo.setLaneNum3(data_eachline[5]);
						vo.setLaneNum4(data_eachline[6]);
						vo.setLaneNum5(data_eachline[7]);
					}else if(data_eachline.length == 9){
						vo.setLaneNum2(data_eachline[4]);
						vo.setLaneNum3(data_eachline[5]);
						vo.setLaneNum4(data_eachline[6]);
						vo.setLaneNum5(data_eachline[7]);
						vo.setLaneNum6(data_eachline[8]);
					}else if(data_eachline.length == 10){
						vo.setLaneNum2(data_eachline[4]);
						vo.setLaneNum3(data_eachline[5]);
						vo.setLaneNum4(data_eachline[6]);
						vo.setLaneNum5(data_eachline[7]);
						vo.setLaneNum6(data_eachline[8]);
						vo.setLaneNum7(data_eachline[9]);
					}else if(data_eachline.length == 11){
						vo.setLaneNum2(data_eachline[4]);
						vo.setLaneNum3(data_eachline[5]);
						vo.setLaneNum4(data_eachline[6]);
						vo.setLaneNum5(data_eachline[7]);
						vo.setLaneNum6(data_eachline[8]);
						vo.setLaneNum7(data_eachline[9]);
						vo.setLaneNum8(data_eachline[10]);
					}else if(data_eachline.length == 12){
						vo.setLaneNum2(data_eachline[4]);
						vo.setLaneNum3(data_eachline[5]);
						vo.setLaneNum4(data_eachline[6]);
						vo.setLaneNum5(data_eachline[7]);
						vo.setLaneNum6(data_eachline[8]);
						vo.setLaneNum7(data_eachline[9]);
						vo.setLaneNum8(data_eachline[10]);
						vo.setLaneNum9(data_eachline[11]);
					}else if(data_eachline.length == 13){
						vo.setLaneNum2(data_eachline[4]);
						vo.setLaneNum3(data_eachline[5]);
						vo.setLaneNum4(data_eachline[6]);
						vo.setLaneNum5(data_eachline[7]);
						vo.setLaneNum6(data_eachline[8]);
						vo.setLaneNum7(data_eachline[9]);
						vo.setLaneNum8(data_eachline[10]);
						vo.setLaneNum9(data_eachline[11]);
						vo.setLaneNum10(data_eachline[12]);
					}
					//填充map，注意map的key是dev_id拼接dir（比如3001E2W）
					if(dev_laneMap.containsKey(data_eachline[0]+data_eachline[2])){
						dev_laneMap.get(data_eachline[0]+data_eachline[2]).add(vo);
					}else{
						List<DevAndLane> list = new ArrayList<DevAndLane>();
						list.add(vo);
						dev_laneMap.put(data_eachline[0]+data_eachline[2], list);
					}
				}
				lineNum ++;
			}
			reader.close();
			return dev_laneMap;
		} catch (FileNotFoundException e) {
			System.out.println(e.toString());
			return null;
		} catch (IOException e) {
			System.out.println(e.toString());
			return null;
		}
	}
	
	/**
	 * 获取设备id和rticid、方向的映射
	 * 当前量级：1000多个key，每个key对应一个bean
	 * @return
	 */
	public static HashMap<String,DevAndRtic> getDev_rticMap(String filePath){
		HashMap<String, DevAndRtic> dev_rticMap = new HashMap<String, DevAndRtic>();
		// 获取设备id和rticid、方向的映射
		File file = new File(filePath);
	    BufferedReader reader = null;
	    try {
			reader = new BufferedReader(new FileReader(file));
			String line = null;
			int lineNum = 1;
			String[] data_eachline = null;
			// 一次读入一行，直到读入null为文件结束
			while ((line = reader.readLine()) != null) {
				if(lineNum > 1){
					data_eachline = line.split(",");
					DevAndRtic vo = new DevAndRtic();
					vo.setDev_id(data_eachline[0].substring(1, data_eachline[0].length() - 1));
					vo.setDev_Type(data_eachline[15].substring(1, data_eachline[15].length() - 1));
					vo.setRtic1_id(data_eachline[2].substring(1, data_eachline[2].length() - 1));
					vo.setDir1(data_eachline[3].substring(1, data_eachline[3].length() - 1));
					vo.setRtic2_id(data_eachline[4].substring(1, data_eachline[4].length() - 1));
					vo.setDir2(data_eachline[5].substring(1, data_eachline[5].length() - 1));
					vo.setRtic3_id(data_eachline[6].substring(1, data_eachline[6].length() - 1));
					vo.setDir3(data_eachline[7].substring(1, data_eachline[7].length() - 1));
					vo.setRtic4_id(data_eachline[8].substring(1, data_eachline[8].length() - 1));
					vo.setDir4(data_eachline[9].substring(1, data_eachline[9].length() - 1));
					vo.setRtic_num(countRtic(vo));
					dev_rticMap.put(data_eachline[0].substring(1, data_eachline[0].length() - 1), vo);
				}
				lineNum ++;
			}
			reader.close();
			
			//测试
			/*for(String key: dev_rticMap.keySet()){
				if(dev_rticMap.get(key).getRtic_num().equals("2")){
					System.out.println(key + "--2");
				}
				if(dev_rticMap.get(key).getRtic_num().equals("3")){
					System.out.println(key + "--3");
				}
			}*/
			
			return dev_rticMap;
	    } catch (FileNotFoundException e) {
			System.out.println(e.toString());
			return null;
		} catch (IOException e) {
			System.out.println(e.toString());
			return null;
		}
	}
	
	/**
	 * 获取rtic和road映射关系
	 * @param file1
	 * @return
	 */
	public static HashMap<String,String> getRoad_RoadSectionMap(String file1){
		File file = new File(file1);
	    BufferedReader reader = null;
	    HashMap<String,String> rtic_road = new HashMap<String, String>();
		try {
			reader = new BufferedReader(new FileReader(file));
			String line = null;
			int lineNum = 1;
			String[] data_eachline = null;
			// 一次读入一行，直到读入null为文件结束
			while ((line = reader.readLine()) != null) {
				if(lineNum > 1){
					data_eachline = line.split(",");
					rtic_road.put(data_eachline[2], data_eachline[0]);
				}
				lineNum++;
			}
			reader.close();
//			System.out.println(lineNum + " lines total!");
//			System.out.println("Job finished!");
			return rtic_road;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}
	
	private static String countRtic(DevAndRtic vo){
		String num = "";
		int count = 0;
		if(null != vo.getRtic1_id() && !vo.getRtic1_id().equals("")){
			count ++;
		}
		if(null != vo.getRtic2_id() && !vo.getRtic2_id().equals("")){
			count ++;
		}
		if(null != vo.getRtic3_id() && !vo.getRtic3_id().equals("")){
			count ++;
		}
		if(null != vo.getRtic4_id() && !vo.getRtic4_id().equals("")){
			count ++;
		}
		num = String.valueOf(count);
		return num;
	}

	/**
	 * 获取rtic粒度的通行能力
	 * @param filePath
	 * @return
	 */
	public static HashMap<String, String> getRtic_capacityMap(String filePath) {
		
		File file = new File(filePath);
	    BufferedReader reader = null;
	    HashMap<String, String> rtic_capacityMap = new HashMap<String, String>();
		try {
			reader = new BufferedReader(new FileReader(file));
			String line = null;
			int lineNum = 1;
			String[] data_eachline = null;
			// 一次读入一行，直到读入null为文件结束
			while ((line = reader.readLine()) != null) {
				if(lineNum > 1){
					data_eachline = line.split(",");
					rtic_capacityMap.put(data_eachline[0], data_eachline[1]);
				}
				lineNum++;
			}
			reader.close();
			return rtic_capacityMap;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	/**
	 * 获取road粒度的通行能力
	 * @param filePath
	 * @return
	 */
	public static HashMap<String, String> getRoad_capacityMap(String filePath) {
		
		File file = new File(filePath);
	    BufferedReader reader = null;
	    HashMap<String, String> road_capacityMap = new HashMap<String, String>();
		try {
			reader = new BufferedReader(new FileReader(file));
			String line = null;
			int lineNum = 1;
			String[] data_eachline = null;
			// 一次读入一行，直到读入null为文件结束
			while ((line = reader.readLine()) != null) {
				if(lineNum > 1){
					data_eachline = line.split(",");
					road_capacityMap.put(data_eachline[0], data_eachline[1]);
				}
				lineNum++;
			}
			reader.close();
			return road_capacityMap;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}
	
	/**
	 * 加载rticid和rtickind映射关系
	 * @param filePath
	 * @return
	 */
	public static HashMap<String, String> loadRticKind(String filePath){
		HashMap<String, String> rtic_kind_map = new HashMap<String, String>();
		File file = new File(filePath);
	    BufferedReader reader = null;
	    try {
			reader = new BufferedReader(new FileReader(file));
			String line = null;
			int lineNum = 1;
			String[] data_eachLine = null;
			String rticid = "";
			String rticKind = "";
			// 一次读入一行，直到读入null为文件结束
			while ((line = reader.readLine()) != null) {
				if(lineNum > 1){
					data_eachLine = line.split(",");
					if(data_eachLine.length > 1){
						rticid = data_eachLine[0].substring(1, data_eachLine[0].length() - 1);
						rticKind = data_eachLine[5].substring(1, 2);
						rtic_kind_map.put(rticid, rticKind);
					}
				}
				lineNum ++;
			}
			reader.close();
			//System.out.println(rtic_kind_map.size());
			return rtic_kind_map;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}
	
	/**
	 * 获取rticid和rtic自由流速度映射
	 * @param fileName
	 * @return
	 */
	public static HashMap<String,String> loadRticFreeFlowSpeed(String fileName){
		File file = new File(fileName);
	    BufferedReader reader = null;
	    HashMap<String,String> rtic_freeFlowSpeed_map = new HashMap<String,String>();
	    try {
			reader = new BufferedReader(new FileReader(file));
			String line = null;
			int lineNum = 1;
			String[] data_eachLine = null;
			String rticid = "";
			String freeFlowSpeed = "";
			// 一次读入一行，直到读入null为文件结束
			while ((line = reader.readLine()) != null) {
				if(lineNum > 1){
					data_eachLine = line.split(",");
					if(data_eachLine.length > 1){
						rticid = data_eachLine[0];
						freeFlowSpeed = data_eachLine[1];
						rtic_freeFlowSpeed_map.put(rticid, freeFlowSpeed);
					}
				}
				lineNum ++;
			}
			reader.close();
			//System.out.println(rtic_kind_map.size());
			return rtic_freeFlowSpeed_map;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}
	
	/**
	 * 获取rtic和车道数的关系
	 * @param fileName
	 * @return
	 */
	public static HashMap<String,String> loadRticLaneNum(String fileName){
		File file = new File(fileName);
	    BufferedReader reader = null;
	    HashMap<String,String> rtic_LaneNumMap = new HashMap<String,String>();
	    try {
			reader = new BufferedReader(new FileReader(file));
			String line = null;
			int lineNum = 1;
			String[] data_eachLine = null;
			String rticid = "";
			String freeFlowSpeed = "";
			// 一次读入一行，直到读入null为文件结束
			while ((line = reader.readLine()) != null) {
				if(lineNum > 1){
					data_eachLine = line.split(",");
					if(data_eachLine.length > 1){
						rticid = data_eachLine[0];
						freeFlowSpeed = data_eachLine[1];
						rtic_LaneNumMap.put(rticid, freeFlowSpeed);
					}
				}
				lineNum ++;
			}
			reader.close();
			//System.out.println(rtic_kind_map.size());
			return rtic_LaneNumMap;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}
//	public static void main(String[] args) {
//		HashMap<String, List<DevAndLane>> dev_laneMap = getDev_laneMap("D:\\data\\西安通行能力分析\\文档\\匹配表\\T_DEVID_LANEID.csv");
//		for(String key: dev_laneMap.keySet()){
//			if(key.equals("3034S2N")){
//				List<DevAndLane> list = dev_laneMap.get(key);
//				System.out.println("");
//			}
//			if(key.equals("3034N2S")){
//				List<DevAndLane> list = dev_laneMap.get(key);
//				System.out.println("");
//			}
//			if(key.equals("3034W2E")){
//				List<DevAndLane> list = dev_laneMap.get(key);
//				System.out.println("");
//			}
//			if(key.equals("3034E2W")){
//				List<DevAndLane> list = dev_laneMap.get(key);
//				System.out.println("");
//			}
//		}
//		HashMap<String, DevAndRtic> dev_rticMap = getDev_rticMap("D:\\data\\西安通行能力分析\\文档\\匹配表\\采集设备toRTIC映射表V3_修改方向汉字.csv");
//		for(String key: dev_rticMap.keySet()){
//			if(key.equals("3034")){
//				DevAndRtic vo = dev_rticMap.get(key);
//				System.out.println("");
//			}
//		}
//	}

}
